<?php
abstract class BasePhoto extends Object
{
    protected $config;
    protected $dir;
    protected $url;

    public $dirOrigin;
    public $urlOrigin;

    public $SIZE_TINY;
    public $SIZE_THUMB;
    public $SIZE_SMALL;
    public $SIZE_MEDIUM;
    public $SIZE_LARGE;

    public function BasePhoto()
    {
        parent::__construct();
        $this->config = $this->GetConfig();
        $this->dir = $this->config->DIR_PHOTO;
        $this->url = $this->config->URL_PHOTO;
        $this->dirOrigin = $this->dir.DS.'origin';
        $this->urlOrigin = $this->url.FS.'origin';
        $this->LoadSizes();
    }

    abstract public function GetConfig();

    abstract protected function LoadSizes();

    public function DownloadImage($img_name)
    {
        if (!$img_name) return;

        $file = AppConfig::GetInstance()->URL_DOWNLOAD_IMG.FS.$img_name;
        $newfile = $this->dirOrigin.DS.$img_name;
        FileHandler::MakeDir($this->dirOrigin);
        try
        {
            FileHandler::StreamCopy($file, $newfile);
        }
        catch (Exception $ex)
        {}
    }

    public function GetUrl($name, $size = null)
    {
        $file = $this->dirOrigin.DS.$name;
        if (!file_exists($file) && AppConfig::GetInstance()->APP_DOWNLOAD_IMG_MODE) $this->DownloadImage($name);

        $image = new Image($file);
        if (!$size)
        {
            $url = new Url($this->urlOrigin.FS.$name);
        }
        else if ($image->IsValid())
        {
            try
            {
                $rs_dir = $size->width.'x'.$size->height;
                $rs_path = $this->dir.DS.$rs_dir;
                $rs_file = $rs_path.DS.$name;

                $refresh = file_exists($rs_file) ? filemtime($image->file) > filemtime($rs_file) || AppConfig::GetInstance()->IsClearCache() : true;
                if ($refresh)
                {
                    FileHandler::MakeDir($rs_path);
                    $image->Resize($size->width, $size->height, $rs_path);
                }
                $url = new Url($this->url.FS.$rs_dir.FS.$name);
            }
            catch (Exception $ex)
            {
                $url = new Url();
            }
        }
        else
        {
            $url = new Url();
        }

        return $url->GetCdn();
    }

    public function GetTinyUrl($name)
    {
        return $this->GetUrl($name, $this->SIZE_TINY);
    }

    public function GetThumbUrl($name)
    {
        return $this->GetUrl($name, $this->SIZE_THUMB);
    }

    public function GetSmallUrl($name)
    {
        return $this->GetUrl($name, $this->SIZE_SMALL);
    }

    public function GetMediumUrl($name)
    {
        return $this->GetUrl($name, $this->SIZE_MEDIUM);
    }

    public function GetLargeUrl($name)
    {
        return $this->GetUrl($name, $this->SIZE_LARGE);
    }

    public function Delete($name)
    {
        $subs = FileHandler::GetSubDirs($this->dir);
        foreach ($subs as $sub)
        {
            try
            {
                FileHandler::Delete($this->dir.DS.$sub.DS.$name);
            }
            catch (Exception $ex)
            {
                MessageBase::Warning($ex->getMessage());
            }
        }
    }

    public function GetPhotos($limit = null, &$page, &$count = -1)
    {
        return self::GetFilesHelper($this->dirOrigin, $limit, $page, $count);
    }

    public static function GetFilesHelper($dirname, $limit = null, &$page = null, &$count = -1)
    {
        $dir = FileHandler::DirOpen($dirname);
        $out = array();
        $i = 0;
        $floor = ($page - 1) * $limit;
        $ceiling = $floor + $limit;
        while ($file = readdir($dir))
        {
            if (is_file($dirname.DS.$file))
            {
                if (!$limit || ($i >= $floor && $i < $ceiling)) $out[] = array('id' => $file);
                $i++;
                if ($i == $ceiling && $count == -1) break;
            }
        }
        closedir($dir);
        $count = $i;
        return $out;
    }
}
?>
